//
//  ZBMoBanNineViewController.m
//  MoBan
//
//  Created by apple on 16/7/18.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ZBMoBanNineViewController.h"
#import "ZBMoBanNineCollectionViewCell.h"
@interface ZBMoBanNineViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong)UICollectionView *collectionview;
@property(nonatomic,strong)NSMutableArray *array;
@property(nonatomic,strong)NSMutableArray *array2;

@end

@implementation ZBMoBanNineViewController
-(NSMutableArray *)array2
{
    if (!_array2) {
        _array2 = [NSMutableArray array];
    }
    return _array2;
}
-(NSMutableArray *)array
{
    if (!_array) {
        _array = [NSMutableArray array];
    }
    return _array;
}

-(UICollectionView *)collectionview
{
    if (!_collectionview) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        layout.minimumInteritemSpacing = 10*Kscr;
        layout.minimumLineSpacing = 10*Kscr;
    
        _collectionview = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, KscrW, KscrH-KBar1) collectionViewLayout:layout];
        _collectionview.backgroundColor = [UIColor whiteColor];
        _collectionview.dataSource = self;
        _collectionview.delegate = self;
        [_collectionview registerClass:[ZBMoBanNineCollectionViewCell class] forCellWithReuseIdentifier:@"ZBMoBanNineCollectionViewCell"];
    }
    return _collectionview;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.collectionview];
    [self setUpMovement];
    
    for (NSInteger i=0; i<20; i++) {
        [self.array addObject:[NSString stringWithFormat:@"%ld",i]];
        [self.array2 addObject:[NSString stringWithFormat:@"%ld",i]];
    }
    // Do any additional setup after loading the view.
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section==0) {
        return self.array.count;
    }
    
    
    return self.array2.count;

}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZBMoBanNineCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZBMoBanNineCollectionViewCell" forIndexPath:indexPath];
    [cell ZBSetDatasouce:self.array[indexPath.item]];
    return cell;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(80*Kscr, 40*Kscr);

}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10*Kscr, 5*Kscr, 0, 5*Kscr);

}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(KscrW, 30*Kscr);
}
/***重排功能***/
- (void)setUpMovement{
    UILongPressGestureRecognizer *longPressGestureRecognizer =             [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
    [self.collectionview addGestureRecognizer:longPressGestureRecognizer];
}
- (void)longPressAction:(UIGestureRecognizer *)gesture{
    NSIndexPath *indexPath = [self.collectionview indexPathForItemAtPoint:[gesture locationInView:self.collectionview]];
    
//    if (indexPath.section==0) {
        switch (gesture.state) {
            case UIGestureRecognizerStateBegan:{
                //当长按手势刚开始时，开始执行移动操作
                
                NSArray *array =[self.collectionview visibleCells];
                for (ZBMoBanNineCollectionViewCell *cell in array) {
                    [cell ZBchangeFram:1];
                }
                
                [self.collectionview beginInteractiveMovementForItemAtIndexPath:indexPath];
                break;
            }
            case UIGestureRecognizerStateChanged:{
                //当长按手势改变位置时，执行移动的操作
                //下面两个状态，方法和意思相同
                
                
                [self.collectionview updateInteractiveMovementTargetPosition:[gesture locationInView:self.collectionview]];
                
                break;
            }
            case UIGestureRecognizerStateEnded:{
                NSArray *array =[self.collectionview visibleCells];
                for (ZBMoBanNineCollectionViewCell *cell in array) {
                    [cell ZBchangeFram:0];
                }

                [self.collectionview endInteractiveMovement];
                break;
            }
            default:
                [self.collectionview cancelInteractiveMovement];
                break;
        }

//    }
}
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
    
    if (sourceIndexPath.section==destinationIndexPath.section&&sourceIndexPath.section==0) {
        [self ZBRemoveIteamIn:sourceIndexPath.row WithArray:self.array ToArray:self.array atIndex:destinationIndexPath.row];

    }else if (sourceIndexPath.section==destinationIndexPath.section&&sourceIndexPath.section==1)
    {
        [self ZBRemoveIteamIn:sourceIndexPath.row WithArray:self.array2 ToArray:self.array2 atIndex:destinationIndexPath.row];

    
    }else
    {
        if (sourceIndexPath.section==0) {
            [self ZBRemoveIteamIn:sourceIndexPath.row WithArray:self.array ToArray:self.array2 atIndex:destinationIndexPath.row];
            
        }else
        {
        
            [self ZBRemoveIteamIn:sourceIndexPath.row WithArray:self.array2 ToArray:self.array atIndex:destinationIndexPath.row];

        }
    }
    
}
-(void)ZBRemoveIteamIn:(NSInteger)IndexPathRow WithArray:(NSMutableArray *)array ToArray:(NSMutableArray *)addrray atIndex:(NSInteger )inter
{
    NSString *temp = [array objectAtIndex:IndexPathRow];
    [array removeObjectAtIndex:IndexPathRow];
    [addrray insertObject:temp atIndex:inter];


}


@end
